//
//  ADOriginalCell.swift
//  myBlog
//
//  Created by mac2014 on 16/3/28.
//  Copyright © 2016年 AD. All rights reserved.
//

import UIKit
import SnapKit

//原创微博cell

class ADOriginalView: UIView {
    
    //MARK: - 属性
    var originalBottomCon: Constraint?
    //微博模型
    var status: ADStatus? {
        didSet {
            //头像
            if let imageURL = status?.user?.profile_image_url
            {
                let url = NSURL(string: imageURL)
                iconView.sd_setImageWithURL(url)
            }
            //用户名
            userName.text = status?.user?.screen_name
            
            //来源
            sourceLabel.text = status?.source
            
            //创建时间
            timeLabel.text = status?.created_at
            
            //会员等级图标
            memberIcon.image = status?.user?.memberImage
            
            /// 认证图标
            //certificateImage.image =
            certificateImage.image = status?.user?.certificateImgage
            
            //文本内容
            contentLabel.text = status?.text
            
            //微博状态配图：有则显示，没有则不显示
            self.originalBottomCon?.uninstall()
            if let picURL = status?.storePictureURLs {
                //有图
                pictureView.hidden = false
                pictureView.picURLs = picURL
                //添加约束
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.originalBottomCon = make.bottom.equalTo(pictureView).offset(8).constraint
                })
                
            }else {
                //没有图
                pictureView.hidden = true
                //原创底部和文本框相距8
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.originalBottomCon = make.bottom.equalTo(contentLabel).offset(8).constraint
                })
            }
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        
        //self.backgroundColor = UIColor.random()
        prepareUI()
    }
    
    private func prepareUI() {
        // 添加子控件
        addSubview(iconView)
        addSubview(userName)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(memberIcon)
        addSubview(certificateImage)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        // 添加约束
        /// 用户头像
        iconView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(8)
            make.top.equalTo(self).offset(8)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        
        /// 用户名称
        userName.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView.snp_trailing).offset(8)
            make.top.equalTo(iconView)
        }
        
        /// 时间
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView.snp_trailing).offset(8)
            make.bottom.equalTo(iconView)
        }
        
        /// 来源
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(8)
            make.centerY.equalTo(timeLabel)
        }
        
        /// 会员等级
        memberIcon.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(userName.snp_trailing).offset(8)
            make.centerY.equalTo(userName)
            make.size.equalTo(CGSize(width: 14, height: 14))
        }
        
        /// 认证图标
        certificateImage.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_trailing)
            make.centerY.equalTo(iconView.snp_bottom)
            make.size.equalTo(CGSize(width: 17, height: 17))
        }
        
        /// 微博文本内容
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_bottom).offset(8)
            make.leading.equalTo(self).offset(8)
            make.trailing.equalTo(self).offset(-8)
        }
        
        ///微博配图
        pictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView)
            make.top.equalTo(contentLabel.snp_bottom).offset(10)
            make.size.equalTo(CGSize(width: 150, height: 150))
        }
        
        /// 原创cell的底部参照contentLabel的底部
        self.snp_makeConstraints { (make) -> Void in
            self.originalBottomCon = make.bottom.equalTo(pictureView).offset(8).constraint
            
        }
    }
    
    
    
    //MARK: - 懒加载
    //头像：iconView
    lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar"))
    
    //用户名：userName
    lazy var userName: UILabel = {
        let userName = UILabel()
        userName.textColor = UIColor.darkGrayColor()
        userName.font = UIFont.systemFontOfSize(14)
        return userName
    }()
    
    //微博内容：contentLabel
    private lazy var contentLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFontOfSize(16)
        label.textColor = UIColor.darkGrayColor()
        label.numberOfLines = 0
        return label
    }()
    
    //认证图标：certificateImage
    private lazy var certificateImage: UIImageView = UIImageView(image: UIImage(named: "avatar_grassroot"))
    
    //会员等级
    private lazy var memberIcon: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership_expired"))
    
    //时间：timeLabel
    private lazy var timeLabel: UILabel = {
        let timeLabel = UILabel()
        timeLabel.textColor = UIColor.redColor()
        timeLabel.font = UIFont.systemFontOfSize(12)
        return timeLabel
    }()
    
    //来源：sourceLabel
    private lazy var sourceLabel: UILabel = {
        let sourceLabel = UILabel()
        sourceLabel.textColor = UIColor.redColor()
        sourceLabel.font = UIFont.systemFontOfSize(12)
        return sourceLabel
    }()
    
    //微博配图
    private lazy var pictureView: ADPictureView = ADPictureView()
    
}
